package oeg.zsl.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.zsl.model.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisTestor {

    @Test
    public void testSelect() throws IOException {
         InputStream inputStream= Resources.getResourceAsStream("mybatis.cf.xml");
         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        SqlSession sqlSession= sqlSessionFactory.openSession(true);

        String statement = "org.zsl.dao.mybatis.UserMapper.selectAll";
        List<User> userList= sqlSession.selectList(statement);


        for (User user : userList){
            System.out.println(user);
        }

    }


    /**
     *   SqlSessionFactory
     *                  SqlSession ----> DB
     *                       selectList
     *                       insert
     * */
    @Test
    public void testInsert() throws IOException {

        User user = new User();
        user.setUsername("tttt");
        user.setPassword("33");
        user.setRoleId(3);

        InputStream inputStream= Resources.getResourceAsStream("mybatis.cf.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        SqlSession sqlSession= sqlSessionFactory.openSession(true);

        String statement = "org.zsl.dao.mybatis.UserMapper.insert";

        int affected= sqlSession.insert(statement,user);

      ///  sqlSession.commit();

        System.out.println(affected);
    }
}
